package com.shangguosha.model.cards;

import com.shangguosha.model.Character;
import com.shangguosha.model.enums.Suit;

/**
 * 桃卡牌
 */
public class Peach extends BasicCard {
    
    public Peach(String id, Suit suit, int number) {
        super(id, "桃", suit, number, "回复1点体力");
    }

    public Peach(String id, String name, Suit suit, int number) {
        super(id, name, suit, number, "回复1点体力");
    }

    @Override
    public boolean canUse(Character player, Character target) {
        if (player == null) {
            return false;
        }
        
        // 如果没有指定目标，默认对自己使用
        if (target == null) {
            target = player;
        }
        
        // 目标必须存活
        if (!target.isAlive()) {
            return false;
        }
        
        // 只有在体力不满或濒死状态时才能使用桃
        return target.getCurrentHp() < target.getMaxHp() || target.getCurrentHp() <= 0;
    }
    
    @Override
    public void use(Character player, Character target) {
        // 如果没有指定目标，默认对自己使用
        if (target == null) {
            target = player;
        }
        
        if (!canUse(player, target)) {
            return;
        }
        
        // 发出桃的使用事件
        emit("peach_used", player, target, this);
        
        // 回复1点体力
        target.heal(1);
        
        if (player.equals(target)) {
            System.out.printf("%s 使用了【桃】，回复了1点体力%n", player.getName());
        } else {
            System.out.printf("%s 对 %s 使用了【桃】，%s 回复了1点体力%n", 
                    player.getName(), target.getName(), target.getName());
        }
    }
    
    /**
     * 救援濒死角色
     */
    public boolean canSave(Character player, Character dyingCharacter) {
        return player != null && dyingCharacter != null && 
               player.isAlive() && dyingCharacter.getCurrentHp() <= 0 &&
               player.hasCard(this);
    }
    
    /**
     * 执行救援
     */
    public void save(Character player, Character dyingCharacter) {
        if (canSave(player, dyingCharacter)) {
            player.discardCard(this);
            dyingCharacter.heal(1);
            emit("peach_save", player, dyingCharacter, this);
            System.out.printf("%s 使用【桃】救了 %s%n", player.getName(), dyingCharacter.getName());
        }
    }
    
    @Override
    public String getEffectDescription() {
        return "回复1点体力";
    }
    
    @Override
    public Peach clone() {
        return new Peach(getId() + "_clone", getSuit(), getNumber());
    }
}